package controller;

import java.io.IOException;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.faces.context.FacesContext;
import model.dao.PersonDao;
import model.dao.RoleDao;
import model.domain.Person;
import model.domain.Project;
import model.domain.Role;
import model.domain.Status;


@ManagedBean
public class personBean {

    public personBean() {
        this.selectedperson = new Person();
        this.newperson = new Person();
    }

    private Project project;
    private Person newperson;
    private Person selectedperson;

    public void setProject(Project object){
        this.project = object;
    }

    public Project getProject(){
	return this.project;
    }

    public void setSelectedperson(Person person){
        this.selectedperson.setFirstName(person.getFirstName());
        this.selectedperson.setLastName(person.getLastName());
        this.selectedperson.setLogin(person.getLogin());
        this.selectedperson.setPassword(person.getPassword());
        this.selectedperson.setPersonID(person.getPersonID());
        this.selectedperson.setRole(person.getRole());
        this.selectedperson.setImpediments(person.getImpediments());
        this.selectedperson.setProjectparticipations(person.getProjectparticipations());
        this.selectedperson.setSprintbacklogs(person.getSprintbacklogs());
        this.selectedperson.setStatus(person.getStatus());
    }

    public Person getSelectedperson(){
        return this.selectedperson;
    }

    public void setNewperson(Person person){
        this.newperson = person;
    }

    public Person getNewperson(){
        return this.newperson;
    }

    public List<Person> getUsers(){
        PersonDao home = new PersonDao();
        return home.getAll();
    }

    public List<Person> getActiveusers(){
        Status status = new Status();
        status.setStatusID(4);

        PersonDao home = new PersonDao();
        return home.findByStatus(status);
    }

    public List<Role> getRoles(){
        RoleDao home = new RoleDao();
        return home.getAll();
    }

    public void submitCreate() throws IOException{

        PersonDao home = new PersonDao();

        Person auxPerson = new Person();
        auxPerson.setFirstName(this.newperson.getFirstName());
        auxPerson.setLastName(this.newperson.getLastName());
        auxPerson.setLogin(this.newperson.getLogin());
        auxPerson.setPassword(this.newperson.getPassword());
        auxPerson.setRole(this.newperson.getRole());
        auxPerson.setStatus(this.newperson.getStatus());

        home.insert(auxPerson);

        this.newperson.setFirstName("");
        this.newperson.setLastName("");
        this.newperson.setLogin("");
        this.newperson.setPassword("");
        this.newperson.setRole(null);
        this.newperson.setStatus(null);

        FacesContext.getCurrentInstance().getExternalContext().redirect("People.xhtml");
    }

    public void submitEdit(){

        PersonDao home = new PersonDao();
        home.insertOrUpdate(this.selectedperson);
    }

    public void submitDelete(){
        PersonDao personHome = new PersonDao();
        personHome.delete(this.selectedperson);
    }

}
